<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Talepler extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct()
    {
        parent::__construct();
            // Your own constructor code
        
        
    } 
    
 public function tq3($category_id , $subcategory_id)
    {
     

  $sql = "select DISTINCT brands.brand_id, brands.name from products, brands where products.category_id = $category_id and products.subcategory_id = $subcategory_id and products.brand_id = brands.brand_id ";

        $data = $this->db->query($sql)->result_array();

        echo json_encode( $data );
    }

    public function tq2($category_id)
    {
        $data = $this->db->select(array('name','subcategories_id'))->where('category_id' ,$category_id)->get('subcategories')->result_array();
       /* echo "<pre>";
        var_dump($data);
    die();*/
        echo json_encode( $data );
    }

    public function tq()
    {
        $data = $this->db->select(array('name','category_id'))->get('categories')->result_array();
       /* echo "<pre>";
        var_dump($data);
    die();*/
        echo json_encode( $data );
    }

    public function test()
    {
        $this->session->unset_userdata('talepler_hemenal');
     /*   echo "<pre>";
        var_dump($this->input->post());
       die(); */
        foreach($this->input->post('products') as $product)
        {
            foreach($this->input->post('adet') as $key => $value)
            {
               // echo "<br>key: $key !?!?? value: $product<br>";
                if($key == $product)
                {
                    // do the magic
                 //   echo "magic<br>";
                    $sql = "select * from seller where seller_id = ?";
                    $result = $this->db->query($sql,$product)->row(0);
                    if($result->fiyat_araligi_id != 0)
                    {
                        $fiyat_araligi = $this->db->where('fiyat_araligi_id',$result->fiyat_araligi_id)->get('fiyat_araligi')->row(0);
                        if($value[0] > $fiyat_araligi->aralik12)
                        {
                            $user_price = $fiyat_araligi->fiyat2;
                        }
                        else
                        {
                            $user_price = $fiyat_araligi->fiyat1;
                        }
                    }
                    else
                    {
                        $user_price = $result->user_price;
                    }
                    $myarray = array(
                        'seller_id' => $result->seller_id,
                        'adet'      => $value[0],
                        'toplam'    => ($value[0] * $user_price),
                        'teklif_id' => $this->input->post('teklif_id')
                       
                    );
                    $user_info[] = $myarray;
                    
                }
            }
            
    
        }
    //     echo "<pre>";
      //      var_dump($user_info);
           // die();
            $this->session->set_userdata('talepler_hemenal',$user_info);
            $ref = $this->input->server('HTTP_REFERER', TRUE);
    redirect($ref, 'location');
      //  var_dump($names);
    
       // $this->session->set_userdata()
    }
    
    public function kategoriler()

	{

        $this->load->view('kategori_talepler');
    //  
       /* $this->db->update('products', array(

            'image1' => "http://tuna.icmproje.com/public/uploads/urun_1.jpg",

            'image2' => "http://tuna.icmproje.com/public/uploads/urun_1.jpg",

            'image3' =>  "http://tuna.icmproje.com/public/uploads/urun_2.jpg",

            'image4'   => "http://tuna.icmproje.com/public/uploads/urun_3.jpg",

            'image5'    => "http://tuna.icmproje.com/public/uploads/urun_4.jpg"

        ));*/

	}
        	
    public function index()
    {  $data['subview'] = 'talepler';
        $this->load->view('layouts/default3',$data);
        
    }
    public function teklifler_all($talep_id)
    {
                      
           $user_id = $this->session->userdata('user_id');
           $sql = "select * from products,talepler where 
           talepler.talep_id = ? and talepler.talep_user_id <> ? 
           and talepler.product_id = products.product_id and talepler.status = 1
           ";
           
           $data['taleplerim'] = $this->db->query($sql,array($talep_id,$user_id))->result();
        $data['subview'] = 'hizli_al_sat/view_hizli_teklifler_all';
        $this->load->view('layouts/default1',$data);   
    }
    public function teklifler($talep_id)
    {
      //  $talepler = $this->db->where('talep_id',$talep_id)->get('talepler')->row(0);
      //  $pro_id = $talepler->product_id;
      //  $user_id = $talepler->talep_user_id;
      //  $sql = "select * from seller,products where seller.product_id = products.product_id and seller.mod != 2 and seller.user_id != ? and seller.product_id = ? ";
      //  $data['taleplerim'] = $this->db->query($sql,array($user_id,$pro_id))->result();
       // echo $this->db->last_query();
      //  $data['taleplerim'] = $this->db->query($sql,$talep_id)->result();

        $data['taleplerim'] = $this->hizlialsat_model->get_user_talepler($this->session->userdata('user_id'),$talep_id)->result();  
         
        $data['subview'] = 'hizli_al_sat/view_hizli_teklifler';
        $this->load->view('layouts/default1',$data); 
    }
   
public function kategori_sec()
{
  
    if($this->input->post('category_id') == "" || $this->input->post('subcategory_id') == "" || $this->input->post('brand_id') == "")
    {
  
       $data['subview'] = 'talepler';
       $data['error'] = "<small>LUTFEN BUTUN ALANLARI SECTIGINIZDEN EMIN OLUN.</small>";
        $this->load->view('layouts/default3',$data);
    }
    else
    {
       redirect(base_url().'talepler/talep_et/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id').'/1','location', 301);
    
    }

                
            
    //$this->output->set_header('Location: ' .base_url().'kategoriler/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id')); 
    
}
public function talep_et($category_id,$subcategory_id,$brand_id,$satis_type)
{
        // satis type = 0 ->normal satis
        // satis type = 1 ->hizli satis
                $category_id = $category_id; //$this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
                $subcategory_id = $subcategory_id; //$this->mycategories->get_subcategory_name($this->config_model->get_subcategories($category_id)->result(),$subcategory_name);
                $brand_id = $brand_id; //$this->mycategories->get_brand_name($this->config_model->get_brands()->result(),$brand_name);
              
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
                
                $data['subcategory_id'] = $subcategory_id;
                $data['subcategory_name'] = $this->config_model->get_subcategory_name($subcategory_id)->row(0)->name;
                
                $data['brand_id'] = $brand_id;
                $data['brand_name'] = $this->config_model->get_brand_name($brand_id)->row(0)->name;
                
                $pag_url = base_url().'talepler/talep_et/'.$category_id."/".$subcategory_id."/".$brand_id.'/'.$satis_type;
                $per_page = $this->_pagination($category_id,$subcategory_id,$brand_id,$pag_url);
               // $data['list_products'] = $this->_capture_products($category_id,$subcategory_id,$brand_id, $per_page,$this->uri->segment(6));
                
                
                $this->db->limit($per_page, $this->uri->segment(7));
                $data['products'] =     $this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->result();
                
            /*    echo "<pre>";
                var_dump($data['products']);
                die();*/
            
                
                   $data['subview'] = 'view_list_products2_talep_et';
                   $this->load->view('layouts/default3',$data);  
                
}
  public function _pagination($category_id,$subcategory_id,$brand_id,$page_url)
    {
  		
		$config['base_url'] = $page_url;
	
        $config['total_rows'] = $this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->num_rows();
		$config['per_page'] = 10;
	//	$config['num_links'] = 2;
        $config['uri_segment'] = '7';
        //$config['page_query_string'] = TRUE;
   //     $config['use_page_numbers'] = TRUE;
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['first_link'] = 'İlk';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Son';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';  
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
        return $config['per_page'];
		
        
		
    }    
public function kategori_sec2()
{
  // normal satis
    if($this->input->post('category_id') == "" || $this->input->post('subcategory_id') == "" || $this->input->post('brand_id') == "")
    {
  
       $data['subview'] = 'new_categories_list';
       $data['error'] = "<small>LUTFEN BUTUN ALANLARI SECTIGINIZDEN EMIN OLUN.</small>";
        $this->load->view('layouts/default3',$data);
    }
    else
    {
        redirect(base_url().'kategori/satis_yap_list/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id').'/0','location', 301);
    }

                
            
    //$this->output->set_header('Location: ' .base_url().'kategoriler/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id')); 
    
}

    public function arama2($value = NULL)
{
   if($value == NULL)
    {
        $value = $this->input->post('k'); 
        $first = true;
    }
    else
    {
        $first = false;
    }
        
    
  
   
    
   // echo "aramadayız k3nk";
    $value = mb_strtolower($value,'UTF-8');
               if($this->db->like('name',$value)->get('brands')->num_rows()>0)
               {
               
                $brand = $this->db->like('name',$value)->get('brands')->row(0)->brand_id;
               
               }


                $value = strtolower($value);
               
               
          
                $this->db->from('products');
               
               // $this->db->where('LOWER(name) LIKE "%'.$value.'%"',NULL,FALSE);
                if(isset($brand))
                {
                    $this->db->or_like('brand_id',$brand);
                }
              $this->db->or_like('name',$value);
                 $this->db->or_like('model', $value);
                if($value  == NULL)
                {
                    $qq = $this->uri->segment(3);
                }
                else
                
                {
                    $qq = $this->uri->segment(4);
                }
                
              
                $this->db->limit(10,$qq);
                
                
                $result = $this->db->get();
                  $pag_url = base_url().'kategori/arama/'.$value.'/';
                 $per_page = $this->_pagination_arama2($value,$pag_url,$first);
             
                if($result->num_rows()> 0)
                {
                      $data['products'] = $result->result();
                        
                    $data['subview'] = 'view_kategori_search_result_satis';
                    $this->load->view('layouts/default3',$data);
                }
                else
                {
                     $data['subview'] = 'view_kategori_search_result_satis';
                 $data['error'] = "Aradığınız kelimeye ait kategori bulunamamıştır.";
                  $this->load->view('layouts/default3',$data);
                }
               
                
                
}
     public function _pagination_arama2($value,$page_url,$first)
    {
  		
		$config['base_url'] = $page_url;
          $value = strtolower($value);
               
                 if($this->db->like('name',$value)->get('brands')->num_rows()>0)
               {
               
                $brand = $this->db->like('name',$value)->get('brands')->row(0)->brand_id;
               
               }


              
               
          
                $this->db->from('products');
               
               // $this->db->where('LOWER(name) LIKE "%'.$value.'%"',NULL,FALSE);
                if(isset($brand))
                {
                    $this->db->or_like('brand_id',$brand);
                }
                  $this->db->or_like('name',$value);
                     $this->db->or_like('model', $value);
                $result_number = $this->db->get()->num_rows();
                
                
              
          
            
        
    
        $config['total_rows'] =  $result_number; //$this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->num_rows();
		$config['per_page'] = 10;
		$config['num_links'] = 2;
        if($first == true)
        {
            $config['uri_segment'] = '3';
        }
        else
        {
             $config['uri_segment'] = '4';
        }
        
        //$config['page_query_string'] = TRUE;
        $config['use_page_numbers'] = TRUE;
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        

        
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
        return $config['per_page'];
		
		
        
		
    } 
    function arama()
    {
         $this->db->insert('ci_query', array('value' => $this->input->post('k')));
	     $query_id = $this->db->insert_id();
         redirect("talepler/display/$query_id");
    }
    function display($query_id = 0,$sort_by = 'model', $sort_order = 'asc', $offset = 0)
    {
        if($this->session->userdata('goster'))
        {
            $limit = $this->session->userdata('goster');
        }
        else
        {
            $limit = 12;
        }
        
        $this->load->model('Search_model');
        $query_array = $this->db->where('id',$query_id)->get('ci_query')->row(0);
      

        

       
          
        
        $results = $this->Search_model->talepler_search($query_array,$limit, $offset, $sort_by, $sort_order,0);
     /*  echo "<pre>";
        var_dump($results->result());
        echo $this->db->last_query();
        die(); */
        
        
        $data['query_id'] = $query_id;
        $this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url("talepler/display/$query_id/$sort_by/$sort_order");
		$config['total_rows'] = $this->Search_model->talepler_search($query_array,$limit, $offset, $sort_by, $sort_order,1)->num_rows();
		$config['per_page'] = $limit;
		$config['uri_segment'] = 6;
        
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['first_link'] = 'İlk';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Son';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';  
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
      
		$this->pagination->initialize($config);
		
	
		$data['sort_by'] = $sort_by;
		$data['sort_order'] = $sort_order;
		$data['pagination'] = $this->pagination->create_links();
		$data['products'] = $results->result();
        $data['total'] = $this->Search_model->talepler_search($query_array, $limit, $offset, $sort_by, $sort_order,1)->num_rows();
        $data['all_products'] = $this->Search_model->talepler_search($query_array,$limit, $offset, $sort_by, $sort_order,1)->result();
        $data['subview'] = 'hizli_al_sat/view_talepler_search';
        $this->load->view('layouts/default1',$data); 
                    
                    
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */